package com.whfc.openapi.api.face.controller;

import com.whfc.common.enums.RecMode;
import com.whfc.common.util.DateUtil;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description 沃土闸机回调
 * @Author hw
 * @Date 2021/1/20 16:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/faceGate/wotu")
public class FaceGateWoTuController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaceGateSyncService faceGateSyncService;

    /**
     * 人员照片下发状态回调
     */
    @PostMapping("photoAuth/callback")
    public String photoAuthCallback(@RequestParam(value = "personGuid", required = false) String personGuid,
                                    @RequestParam(value = "faceGuid", required = false) String faceGuid,
                                    @RequestParam(value = "appId", required = false) String appId,
                                    @RequestParam(value = "deviceKey", required = false) String deviceKey,
                                    @RequestParam(value = "state", required = false) Integer state,
                                    @RequestParam(value = "msg", required = false) String msg,
                                    @RequestParam(value = "time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) {
        logger.info("wotu,人员照片下发状态回调,personGuid:{},faceGuid:{},appId:{},deviceKey:{},state:{},msg:{},time:{}", personGuid, faceGuid, appId, deviceKey, state, msg, DateUtil.formatDateTime(time));

        return "success";
    }

    /**
     * 人员识别回调
     */
    @PostMapping("rec/callback")
    public String recCallback(@RequestParam(value = "personGuid", required = false) String personGuid,
                              @RequestParam(value = "deviceKey", required = false) String deviceKey,
                              @RequestParam(value = "photoUrl", required = false) String photoUrl,
                              @RequestParam(value = "showTime", required = false) String showTime,
                              @RequestParam(value = "data", required = false) String data,
                              @RequestParam(value = "type", required = false) Integer type,
                              @RequestParam(value = "recMode", required = false) Integer recMode,
                              @RequestParam(value = "idCardInfo", required = false) String idCardInfo) {
        logger.info("wotu,人员识别回调,personGuid:{},deviceKey:{},photoUrl:{},showTime:{},data:{},type:{},recMode:{},idCardInfo:{}",
                personGuid, deviceKey, photoUrl, showTime, data, type, recMode, idCardInfo);
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(personGuid);
        dataParam.setDeviceKey(deviceKey);
        dataParam.setPicture(photoUrl);
        if (recMode == null){
            recMode =RecMode.FACE.getValue();
        }
        dataParam.setRecMode(recMode);
        Date time = new Date();
        if (showTime != null) {
            time = DateUtil.parseDate(showTime, DateUtil.DATE_TIME_FORMAT);
        }
        dataParam.setShowTime(time);
        dataParam.setPlatform(FaceGateType.WOTU.getCode());
        faceGateSyncService.syncAttend(dataParam);
        return "success";
    }
}
